'use strict';

/* Controllers */

app
// Flot Chart controller
    .controller('wsAgentCtrl', ['$scope', '$http', '$stateParams', '$rootScope', '$filter', '$location', function ($scope, $http, $stateParams, $rootScope, $filter, $location) {
        $rootScope.clean($scope.app)
        $scope.app.page = {};
        $scope.app.page.name = "ws-agent";
        $scope.app.headerPanel.buttons = [];
        $rootScope.initHeaderElements($scope.settings);
        $rootScope.initBreadcrumb($scope.breadcrumb);
        $scope.breadcrumb.push('产品', 'ws-agent');

        $scope.info = {}
        $scope.info.sett = {};
        $scope.info.brd_projectPlan_is_confirms = [];
        $scope.info.isShow = false;
        var addAgent = {
            show: true,
            label: "创建agent",
            func: function () {
                $scope.add();
            }
        }
        var addWorkspace = {
            show: true,
            label: "创建workspace",
            func: function () {
                $scope.workspaceAdd();
            }
        }
        $scope.header = function () {
            $rootScope.initHeaderElements($scope.settings);
            $scope.settings.btnEle.push(addAgent);
            if ($scope.info.isShow) {
                $scope.settings.btnEle.push(addWorkspace);
            }
        }
        $scope.header();

        $http.get(sys_config_url + '/select/get?type=BRD_PROJECTPLAN_IS_CONFIRM').success(function (data) {
            $scope.info.brd_projectPlan_is_confirms = data.data.BRD_PROJECTPLAN_IS_CONFIRM;
        })
        $http.get(matrix_product_url + '/projectPlan/getProjectPlans?containFinished=N').success(function (data) {
            var list = [];
            angular.forEach(data.data, function (data) {
                if (data.projectPlanStatus == 'NEW') {
                    list.push(data)
                }
            })
            $scope.info.projectPlanList = list;
        })

        $scope.info.agentThData = [{
            "thName": "ip"
        }, {
            "thName": "port"
        }];
        $scope.info.agentList = {
            "tdObjs": [
                {
                    "tdName": "agentIp",
                    "tdType": "text"
                },
                {
                    "tdName": "agentPort",
                    "tdType": "text"
                }
            ],
            "trIsClick": true
        };
        $scope.info.agentList.datas = [];

        $scope.agentList = function () {
            $http.get(sys_config_url + "/agent/get").success(function (data) {
                $scope.info.agentList.datas = data.data;
            })
        }
        $scope.agentList();

        $scope.delete = function (info) {
            if (info.agentIp != null) {
                $http.get(sys_config_url + "/agent/operate?agentId=" + info.agentId).success(function () {
                    if (data.code == 0) {
                        $scope.agentList();
                    } else {
                        alert(data.desc)
                    }
                })
            }

        }
        $scope.save = function (info) {
            $.ajax({
                type: 'post',
                url: sys_config_url + "/agent/update",
                data: JSON.stringify(info),
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.code == 0) {
                        $scope.agentList();
                    } else {
                        alert(data.desc)
                    }
                }
            })
            console.log(info)
        }

        $scope.trcheck = function (info) {
            $scope.info.agentInfo = {};
            if (info.trCheck) {
                $scope.info.agentInfo = info;
                $scope.workspace();
            }
            $scope.info.isShow = info.trCheck;
            $scope.header();
            $scope.info.sett.refresh();
        }

        $scope.add = function () {
            var obj = new Object();
            obj.agentIp = null;
            obj.agentPort = null;
            $scope.info.agentList.datas.push(obj);
        };


        $scope.workspace = function () {
            $scope.info.workspaceThData = [{
                "thName": "代号"
            }, {
                "thName": "路径"
            }, {
                "thName": "是否是默认空间"
            }, {
                "thName": "关联projectPlan"
            }];
            $scope.info.workspaceList = {
                "tdObjs": [
                    {
                        "tdName": "wsCode",
                        "tdType": "text"
                    },
                    {
                        "tdName": "wsPath",
                        "tdType": "text"
                    },
                    {
                        "tdName": "isDefault",
                        "tdType": "select",
                        "typeList": {
                            "list": $scope.info.brd_projectPlan_is_confirms,
                            "key": "key",
                            "value": "value"
                        }
                    },
                    {
                        "tdName": "projectPlanId",
                        "tdType": "select",
                        "typeList": {
                            "list": $scope.info.projectPlanList,
                            "key": "projectPlanId",
                            "value": "projectPlanName"
                        }
                    }
                ],
                "trIsClick": false
            };
            $scope.info.workspaceList.datas = [];
            $http.get(sys_config_url + "/agent/ws/get?agentId=" + $scope.info.agentInfo.agentId).success(function (data) {
                $scope.info.workspaceList.datas = data.data;
            })
        }

        $scope.workspaceDelete = function (info) {
            if (info.wsCode != null) {
                if (info.projectPlanId != undefined || info.projectPlanId != null) {
                    alert("已关联projectPlan，禁止删除操作！");
                    $scope.workspace();
                    return false;
                }
                $http.get(sys_config_url + "/agent/ws/operate?wsId=" + info.wsId).success(function (data) {
                    if (data.code == 0) {
                        $scope.workspace();
                    } else {
                        alert(data.desc)
                    }
                })
            }

        }
        $scope.workspaceSave = function (info) {
            info.agentId = $scope.info.agentInfo.agentId;
            $.ajax({
                type: 'post',
                url: sys_config_url + "/agent/ws/update",
                data: JSON.stringify(info),
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    if (data.code == 0) {
                        $scope.workspace();
                    } else {
                        alert(data.desc)
                    }
                }
            })
        }

        $scope.workspaceAdd = function () {
            var obj = new Object();
            obj.wsCode = null;
            obj.wsPath = null;
            obj.isDefault = null;
            $scope.info.workspaceList.datas.push(obj);
        }

    }])